﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using DG.Tweening;
using QxFramework.Core;
public class EnemyBase : MonoBase
{
    private Vector3 _target;
    private Vector3 _targetDir;
    private Vector3 _targetDir2;
    private int _clock;
    public override void OnEnable()
    {
        base.OnEnable();
        StartCoroutine(OpenUI());
        _clock = 60;
        var path = GetRoadToThePosition(GameMgr.Get<IMonoManager>().GetNearestMonoPos(monoBase, MonoBase.MonoTag.Player));
        if (path == null || path.Count == 0)
            _target = monoBase.Position;
        else
            _target = path[path.Count - 1];

    }
    IEnumerator OpenUI()
    {
        yield return 0;
        UIManager.Instance.Open("EnemyUI", args: gameObject.name);
    }


    List<Vector3> path;
    public override void Update()
    {
        base.Update();
        AnimationControl();

        if (!ItsYourTurn())
        {
            return;
        }

        GameMgr.Get<ICameraManager>().SetBatlleMono(gameObject.name);

        if (GameMgr.Get<IBattleManager>().EnemyEndThisTurnCheck())
        {
            GameMgr.Get<IBattleManager>().EndThisTurn();
        }

        //远离敌人
        if (WithInRage(MonoBase.MonoTag.Enemy, 1f, out bool lostTarget0, out Vector3 otherDirect0, out GameObject Obj0))
        {
            MoveByDirection(-otherDirect0, monoBase.Speed);
        }

        //最近玩家
        if (!WithInRage(MonoBase.MonoTag.Player, monoBase.Range, out bool LostTarget, out Vector3 otherDirect, out GameObject Obj))
        {
            PlayerOutofRange(LostTarget, otherDirect, Obj, GetTarget(MonoBase.MonoTag.Player));
        }
        else
        {
            PlayerInRange(LostTarget, otherDirect, Obj, GetTarget(MonoBase.MonoTag.Player));
        }

        if (!JudgeEnemy())
        {
            return;
        }

        //最近敌人
        if (!WithInRage(MonoBase.MonoTag.Enemy, monoBase.Range, out bool LostTarget2, out Vector3 otherDirect2, out GameObject Obj2))
        {
            EnemyOutofRange(LostTarget2, otherDirect2, Obj2, GetTarget(MonoBase.MonoTag.Enemy));
        }
        else
        {
            EnemyInRange(LostTarget2, otherDirect2, Obj2, GetTarget(MonoBase.MonoTag.Enemy));
        }
    }
    public virtual bool JudgeEnemy()
    {
        return false;
    }
    private Vector3 GetTarget(MonoBase.MonoTag Tag)
    {
        _clock--;
        if (_clock < 0)
        {
            path = GetRoadToThePosition(GameMgr.Get<IMonoManager>().GetNearestMonoPos(monoBase, Tag));
            if (path != null && path.Count != 0)
                _target = path[path.Count - 1];
            _targetDir = (_target - monoBase.Position).normalized;
            _clock = 30;
        }
        return _targetDir;
    }

    public virtual void PlayerOutofRange(bool LostTarget, Vector3 otherDirect, GameObject Obj,Vector3 _targetDir)
    {

    }
    public virtual void PlayerInRange(bool LostTarget, Vector3 otherDirect, GameObject Obj, Vector3 _targetDir)
    {

    }
    public virtual void EnemyOutofRange(bool LostTarget, Vector3 otherDirect, GameObject Obj, Vector3 _targetDir)
    {

    }
    public virtual void EnemyInRange(bool LostTarget, Vector3 otherDirect, GameObject Obj, Vector3 _targetDir)
    {

    }

    public void JudgeLost(Vector3 Dir)
    {
        if (Mathf.Abs(Vector3.Angle(Dir, monoBase.Direction)) > 45)
        {
            monoBase.monoRealtimeStatus.LostingRate = 2f;
        }
    }
    public override bool CDingJudge()
    {
        if (monoBase.MPData.MPValue < monoBase.SkillList[0].skillMPData.MPUse + 1)
        {
            monoBase.monoRealtimeStatus.IsCDing = true;
        }
        if (monoBase.monoRealtimeStatus.IsCDing)
        {
            if (monoBase.MPData.MPValue >= monoBase.MPData.MPCeiling)
            {
                monoBase.monoRealtimeStatus.IsCDing = false;
                monoBase.monoRealtimeStatus.AttackTips = false;
                return true;
            }
            else
            {
                return false;
            }
        }
        return base.CDingJudge();
    }
    public override bool TipsJudge()
    {
        if (!monoBase.monoRealtimeStatus.AttackTips)
        {
            monoBase.monoRealtimeStatus.AttackTips = true;
            monoBase.AttackTipsData.CurrentValue = monoBase.AttackTipsData.ValueCeiling;
        }
        if (monoBase.AttackTipsData.CurrentValue > 0)
        {
            return false;
        }
        return base.TipsJudge();
    }
    public virtual void UseSkillBase(Vector3 Direct, int SkillID, out bool ShootSuccess)
    {
        ShootSuccess = true;
    }
    public override void UseSkill(Vector3 Direct, int SkillID, out bool ShootSuccess)
    {
        base.UseSkill(Direct, SkillID, out ShootSuccess);
        UseSkillBase(Direct, SkillID, out ShootSuccess);
    }
}